/**
 * Created by wangyonghao on 2017/5/7.
 */
angular.module('tribalc').controller('RegisterController', [
    '$scope', 'Api','Alert', function ($scope, Api,Alert) {
        $scope.step = 1;
        $scope.checkOneStep = checkOneStep;
        $scope.judgePasswordSendRequest = judgePasswordSendRequest;
        $scope.getPhoneVerifyCode=getPhoneVerifyCode;
        $scope.judgeVerificationCode=judgeVerificationCode;
        $scope.forResetPassword=forResetPassword;
        $scope.changeVeriCode=changeVerficationPicture;

        $scope.forPasswordInput={};
        getVerPicture();


        function getVerPicture() {
            Api.getVerificationsPicture(function (url,head) {
                $('#verfication').attr('src',url)
                $scope.inputVerNum=head;


            })
        }
        function checkVerNum() {
            if(!$scope.forPasswordInput.verificationCode||$scope.forPasswordInput.verificationCode!==$scope.inputVerNum){
                return false
            }else {
                return true
            }
        }
        function changeVerficationPicture() {
            getVerPicture();
        }

        function judgeVerificationCode(value) {
            if(value.vcode){
                $scope.step+=1
            }else {
                Alert.alertBox(' 请输入正确的验证码')
            }

        }
        function getPhoneVerifyCode(value) {
            if (value&& value.phone){
                Api.sendPhoneVerifyCode(value.phone,function(result){

                })
            }else{
                Alert.alertBox('请输入手机号')
            }


        }

        function checkOneStep(value){
            if(checkOneStepIsEmpty(value)){
                return Alert.alertBox("请填写完整信息")
            }

            if(!checkPhoneRule(value)){
                return Alert.alertBox('请检查手机格式')
            }
            if(!checkVerNum()){
                return Alert.alertBox('请检查输入验证码')
            }
            $scope.step+=1
        }

        function checkPhoneRule(value) {
            var reg = /^1[3|4|5|7|8][0-9]{9}$/;
            return reg.test(value.phone);
        }

        function checkOneStepIsEmpty(value){
            return !value || !value.phone || !value.verificationCode
        }
        function forResetPassword(value) {
            var phone=value.phone;
            var password=value.password;
            var vcode=value.vcode;
            Api.setupForgetPassword(phone, password, vcode, function (result) {
                if(result.code==200){
                    $scope.step+=1;
                }else if(result.code==404){
                    Alert.alertBox('企业未找到')
                }else if(result.code==401){
                    Alert.alertBox('手机或验证码不正确')
                }else if(result.code==400){
                    Alert.alertBox('手机格式不正确')
                }else {
                    Alert.alertBox('')
                }

            },function () {
                Alert.alertBox('请求发送失败')
            })
        }

        function judgePasswordSendRequest (value) {
            if(!value.password || !value.creatPassword){
                Alert.alertBox('请输入密码')
            }else if(value.password==value.creatPassword){
                forResetPassword(value)

            }else {
                Alert.alertBox('密码输入不一致')
            }


        }
    }
]);